package mn.idax.exchange.otc.util;

import mn.idax.exchange.auth.bean.AuthInfo;
import mn.idax.exchange.auth.interfaces.AuthService;
import mn.idax.exchange.auth.util.AuthHelper;
import mn.idax.exchange.common.SpringUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class AuthUtils {

    public static final String TOKEN = "token";

    /**
     * getAuthUserId by token
     *
     * @param token
     * @return
     */
    public static int getAuthUserId(String token) {
        if (StringUtils.isEmpty(token)) {
            return -1;
        }
        AuthHelper authHelper = AuthHelper.getInstance(SpringUtils.getBean(AuthService.class));
        if (!authHelper.isTokenValid(token)) {
            return -1;
        }
        AuthInfo authInfo = authHelper.decodeToken(token);
        if (authInfo == null) {
            return -1;
        }
        return authHelper.decodeToken(token).getId();
    }

    /**
     * getAuthUserId by request
     *
     * @param request
     * @return
     */
    public static int getAuthUserIdWs(HttpServletRequest request) {
        if (request == null) {
            return -1;
        }
        String token = request.getParameter(TOKEN);
        if (token == null) {
            return -1;
        }
        return getAuthUserId(token);
    }

    /**
     * getAuthUserId by request
     *
     * @param request
     * @return
     */
    public static int getAuthUserId(HttpServletRequest request) {
        if (request == null) {
            return -1;
        }
        String token = request.getHeader(TOKEN);
        if (token == null) {
            return -1;
        }
        return getAuthUserId(token);
    }

    /**
     * getAuthUserSource by token
     *
     * @param token
     * @return
     */
    public static int getAuthUserSource(String token) {
        if (StringUtils.isEmpty(token)) {
            return -1;
        }
        AuthHelper authHelper = AuthHelper.getInstance(SpringUtils.getBean(AuthService.class));
        if (!authHelper.isTokenValid(token)) {
            return -1;
        }
        AuthInfo authInfo = authHelper.decodeToken(token);
        if (authInfo == null) {
            return -1;
        }
        return authHelper.decodeToken(token).getSource();
    }

    /**
     * getAuthInfoByToken by request
     *
     * @param request
     * @return
     */
    public static AuthInfo getAuthInfoByToken(HttpServletRequest request) {
        //get token from header first
        String token = request.getHeader(TOKEN);

        if (StringUtils.isEmpty(token)) {
            //get token from parameter second
            token = request.getParameter(TOKEN);
        }

        if (StringUtils.isEmpty(token)) {
            return null;
        }

        AuthHelper authHelper = AuthHelper.getInstance(SpringUtils.getBean(AuthService.class));
        if (!authHelper.isTokenValid(token)) {
            return null;
        }

        return authHelper.decodeToken(token);
    }
}
